package br.edu.flf.graph;

import java.io.File;
import java.io.IOException;
import java.text.Normalizer;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Proba
{
   public static void main(String[] args)
   {
      Proba p = new Proba();
      p.start();
//      p.start2();
   }

   /**
    * Construct a DOT graph in memory, convert it
    * to image and store the image in the file system.
    */
   private void start()
   {
      GraphViz gv = new GraphViz();
      gv.addln(gv.start_graph());
      gv.addln("UNION -> UndergraduateStudent;");
      gv.addln("UNION -> AssistantProfessor2;");
      gv.addln("UNION -> teste;");
      gv.addln("teste -> asdasdas;");
      gv.addln("teste -> aaaaaaaaa;");
      gv.addln(gv.end_graph());
      System.out.println(gv.getDotSource());
      
      String type = "gif";
//      String type = "dot";
//      String type = "fig";    // open with xfig
//      String type = "pdf";
//      String type = "ps";
//      String type = "svg";    // open with inkscape
//      String type = "png";
//      String type = "plain";
      File out = new File("/Users/danieloliveira/FLF/teste." + type);   // Mac
//      File out = new File("/home/danieloliveira/teste." + type);   // Linux
//      File out = new File("c:/eclipse.ws/graphviz-java-api/out." + type);    // Windows
      gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out );
   }
   
   /**
    * Read the DOT source from a file,
    * convert to image and store the image in the file system.
    */
   private void start2()
   {
      String dir = "/home/danieloliveira/teste";     // Linux
      String input = dir + "/sample/simple.dot";
//	   String input = "c:/eclipse.ws/graphviz-java-api/sample/simple.dot";    // Windows
	   
	   GraphViz gv = new GraphViz();
	   gv.readSource(input);
	   System.out.println(gv.getDotSource());
   		
      String type = "gif";
//    String type = "dot";
//    String type = "fig";    // open with xfig
//    String type = "pdf";
//    String type = "ps";
//    String type = "svg";    // open with inkscape
//    String type = "png";
//      String type = "plain";
	   File out = new File("/tmp/simple." + type);   // Linux
//	   File out = new File("c:/eclipse.ws/graphviz-java-api/tmp/simple." + type);   // Windows
	   gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out );
   }
   
   public byte[] getBytesGraph(List<String> lista) {
      GraphViz gv = new GraphViz();
      gv.addln(gv.start_graph());
      
//	  gv.addln(lista.get(0) + " -> " + lista.get(1) + ";");
//	  if (lista.get(2).equals("UNION") || lista.get(2).equals("OPTIONAL") || lista.get(2).equals("AND")) {
//		  gv.addln(lista.get(0) + " -> " + lista.get(2) + ";");
//	  }else {
//		  gv.addln(lista.get(1) + " -> " + lista.get(2) + ";");
//	  }
      
      gv.addln(lista.get(0) + " -> " + lista.get(1) + ";");
      gv.addln(lista.get(1) + " -> " + lista.get(2) + ";");
      gv.addln(lista.get(1) + " -> " + lista.get(3) + ";");
      gv.addln(lista.get(4) + " -> " + lista.get(5) + ";");
      gv.addln(lista.get(4) + " -> " + lista.get(6) + ";");
      gv.addln(gv.end_graph());
      System.out.println(gv.getDotSource());
      
      String type = "pdf";

      byte[] buffer = null;
      
      buffer = gv.getGraph(gv.getDotSource(), type);
      bytesToFile(buffer, type, gv);
      return buffer;      
   }
   
   private void bytesToFile(byte[] buffer, String type, GraphViz gv) {
	   File out = new File("/Users/danieloliveira/FLF/teste." + type);   // Mac
//     File out = new File("/home/danieloliveira/teste." + type);   // Linux
//     File out = new File("c:/eclipse.ws/graphviz-java-api/out." + type);    // Windows
	   gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out );
   }
   
   static public void streamToFile(byte[] buffer, String nomeRelatorio, HttpServletRequest request, HttpServletResponse response) {
		if ((buffer != null) && (buffer.length > 1024)) {
       
			ServletOutputStream outputStream = null;
			try {
				outputStream = response.getOutputStream();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition", "inline;filename=\"" + nomeRelatorio + "\"");
			response.setHeader("Content-transfer-encoding", "binary");
			response.setContentLength(buffer.length);
			
			try {
				outputStream.write(buffer);
				outputStream.close();
				outputStream.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else {
			request.setAttribute("MSGALERT", "Erro - Grafo não gerado");
			request.setAttribute("TIPOALERTA", "alert-danger");
		}
	}
   
   public String removeAcentuacao(String str) {
		 
		str = Normalizer.normalize(str, Normalizer.Form.NFD);
		str = str.replaceAll("[^\\p{ASCII}]", "");
		return str;
		 
	}
}
